// miniprogram/pages/cx/cxPage.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    currentTabId: 'tab0',
    autoScroll: 0,
    isSwip: false,
    flights: ['K102', 'K1033', 'CX142', 'CX888', 'CX999', 'CX287', 'CX839', 'CX187', 'CX123'],
    date: ['Yesterday(11 Nov)', 'Today(11 Nov)', 'Tomorrow(11 Nov)', 'Yesterday(11 Nov)', 'Today(11 Nov)', 'Tomorrow(11 Nov)'],
    planSrc: '../../images/plane_medium.png',
    planSmallSrc: '../../images/plane_small.png',
    listViewHeight: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    const queryTabs = wx.createSelectorQuery();
    queryTabs.select('#listView').boundingClientRect();
    queryTabs.exec(res => {
      const listViewHeight = res[0].height;
      this.setData({
        listViewHeight
      });
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  onSwiperChange: function(event) {
    if (event.detail.source) {
      // console.log(event);
      // const currentTab = event.detail.current;
      // let scrollOffset = 0;
      // const queryTabs = wx.createSelectorQuery();
      // queryTabs.select('#tab' + currentTab).boundingClientRect();
      // queryTabs.exec(res => {
      //   scrollOffset = res[0].left;
      //   console.log(res);

      //   let autoScroll = scrollOffset;
      //   if (this.data.currentTab > this.data.date.length / 2) {
      //     autoScroll = scrollOffset;
      //   } else {
      //     autoScroll = scrollOffset / 2;
      //   }
      //   this.setData({
      //     currentTab,
      //     autoScroll,
      //   })
      // });

      const currentTab = event.detail.current;
      const currentTabId = `tab${currentTab}`
      this.setData({
        currentTab,
        currentTabId
      })
    }

  },

  setIndex: function(event) {
    console.log(event);
    if (this.data.currentTab === event.currentTarget.dataset.current) {
      return false;
    } else {
      // let autoScroll = event.currentTarget.offsetLeft;
      // if (this.data.currentTab > this.data.date.length / 2) {
      //   autoScroll = event.currentTarget.offsetLeft;
      // } else {
      //   autoScroll = event.currentTarget.offsetLeft / 2;
      // }
      // this.setData({
      //   autoScroll: event.currentTarget.offsetLeft,
      //   currentTab: event.currentTarget.dataset.current
      // })
    }

    const currentTab = event.currentTarget.dataset.current;
    const currentTabId = `tab${currentTab}`;
    this.setData({
      currentTabId,
      currentTab
    })


    // this.setData({
    //   autoScroll
    // })
  }
})